-- 302507 Ophelia - face mask 
-- jeong.jihun, jo eunae
-- 2018.10.17

require "Version.lua"

g_face_scale = 0
passThroughNode = nil

function initialize(scene)

  local kuruEngineInstance = KuruEngine.getInstance()
  kuruFaceDetector = KuruFaceDetectorExtension.cast(kuruEngineInstance:getExtension("KuruFaceDetector"))

  kuruTouch = KuruTouchExtension.cast(kuruEngineInstance:getExtension("KuruTouch"))
  kuruTouch:getTouchUpEvent():addEventHandler(onTouchUp)

  EngineStatus.instance():increase(EngineStatusCountType.HAIR_SEGMENTATION)
  hairSegExtension = KuruEngine.getInstance():getExtension("KuruHairSegmentation")
  hairSegExtension = KuruHairSegmentationExtension.cast(hairSegExtension)


  local cameraSnapshot = addNodeAndRelease(scene, KuruSnapshotNode.create())


  -- skinExMask
  headshotMaskBuffer = addNodeAndRelease(scene, KuruFrameBufferNode.createWithScale(0.5, 0.5))
  addChildNodeAndRelease(headshotMaskBuffer, KuruClearNode.create(Vector4.create(0.0, 0.0, 0.0, 0.0)))

  
    passThroughNode = addChildNodeAndRelease(headshotMaskBuffer, KuruShaderFilterNode.createWithFragmentShaderFile(BASE_DIRECTORY .. "convertColor.frag", true))
  

  passThroughNode:setChannel0(hairSegExtension:getMaskSampler())



  local hairSegSnapshot = nil


  hairSegSnapshot = addChildNodeAndRelease(headshotMaskBuffer, KuruSnapshotNode.create())


  g_displayNode = addNodeAndRelease(scene, KuruShaderFilterNode.createWithFragmentShaderFile(BASE_DIRECTORY .. "display.frag", true))
  g_displayNode:setChannel0(cameraSnapshot:getSnapshot())

    g_displayNode:setChannel2(cameraSnapshot:getSnapshot())


  g_displayNode:setChannel3(headshotMaskBuffer:getSampler())
  local cameraSnapshot2 = addNodeAndRelease(scene, KuruSnapshotNode.create())
  scene:addNodeAndRelease(KuruClearNode.create(Vector4.create(0.0, 0.0, 0.0, 0.0)))

  g_displayNode1 = addNodeAndRelease(scene, KuruShaderFilterNode.createWithFragmentShaderFile(BASE_DIRECTORY .. "display1.frag", true))
  g_displayNode1:setChannel0(cameraSnapshot:getSnapshot())

    g_displayNode1:setChannel2(cameraSnapshot:getSnapshot())


  g_displayNode1:setChannel3(headshotMaskBuffer:getSampler())

  local cameraSnapshot3 = addNodeAndRelease(scene, KuruSnapshotNode.create())
  scene:addNodeAndRelease(KuruClearNode.create(Vector4.create(0.0, 0.0, 0.0, 0.0)))

  passThroughNode1 = addNodeAndRelease(scene, KuruShaderFilterNode.createWithFragmentShaderFile(BASE_DIRECTORY .. "passthrough.frag", true))
  

  passThroughNode1:setChannel0(cameraSnapshot2:getSnapshot())
  passThroughNode1:setChannel1(cameraSnapshot3:getSnapshot())
  passThroughNode1:setChannel2(cameraSnapshot:getSnapshot())



end

function frameReady(scene,elapsedTime)
  if hairSegExtension:getMaskSampler() then
    passThroughNode:setChannel0(hairSegExtension:getMaskSampler())
  end
end



function onTouchUp(event)
  eventExtension:postSimpleEventToApp("KaleSimpleEventGetPhotoFromGallery", nil)
end


function reset(scene)

end

function finalize(scene)
  kuruTouch:getTouchUpEvent():removeEventHandler(onTouchUp)
  eventExtension:getSimpleEvent():removeEventHandler(onSimpleEvent)

end

function addChildNodeAndRelease(parent, child)
  parent:addChild(child)
  child:release()
  return child
end

function getBGNode(filePath)
  local bgNode = KuruBackgroundImageNode.create(BASE_DIRECTORY .. filePath, BlendMode.Normal)
  bgNode:setStretch(KuruBackgroundImageNodeStretch.FILL)
  bgNode:setAnchorType(KuruBackgroundImageNodeAnchorType.CENTER)
  return bgNode
end

function getStickerNode(filePath, scale, loactionType, anchorType, translateX, translateY, translateZ)
  local sampler = getAnimationSampler(filePath)
  local node = KaleStickerNode.createFromSampler(sampler, BlendMode.None, 0, 0)

  node:setId("sticker")
  node:setLocationType(loactionType)
  node:setAnchorType(anchorType)
  node:setScale(scale, scale, 0)
  node:setTranslation(translateX, translateY, translateZ)
  sampler:release()

  return node
end

function getAnimationSampler(filePath)
    local sampler = KuruAnimationSampler.createFromPath(BASE_DIRECTORY .. filePath, false, false)
    sampler:setRepeatCount(0)
    sampler:setFPS(20)
    sampler:play()
    sampler:setWrapMode(TextureWrap.CLAMP, TextureWrap.CLAMP)
    return sampler
end

function onAspectRatioChanged(scene)
end

function getBackgroundNode(filePath, scale, anchorType, translateX, translateY, translateZ, blendMode)
    local node = KuruBackgroundImageNode.create(BASE_DIRECTORY .. filePath, blendMode)
    node:setStretch(KuruBackgroundImageNodeStretch.FILL_HORIZONTAL)
    node:setScale(scale, scale, 0)
    node:setAnchorType(anchorType)
    node:setTranslation(translateX, translateY, translateZ)
    return node
end

function getStickerSampler (filePath, fps)
  local sampler = KuruAnimationSampler.createFromPath(getFilePath(filePath), false, false)
  sampler:setRepeatCount(0)
  sampler:setFPS(fps)
  sampler:play()
  return sampler
end

function getFilePath(fileName)
    return BASE_DIRECTORY .. fileName
end

function addNodeAndRelease(scene, node)
  scene:addNode(node)
  node:release()
  return node
end

function addChildAndRelease(parent, child)
  parent:addChild(child)
  child:release()
  return child
end

function split(str, pat)
   local t = {}  -- NOTE: use {n = 0} in Lua-5.0
   local fpat = "(.-)" .. pat
   local last_end = 1
   local s, e, cap = str:find(fpat, 1)
   while s do
      if s ~= 1 or cap ~= "" then
         table.insert(t,cap)
      end
      last_end = e+1
      s, e, cap = str:find(fpat, last_end)
   end
   if last_end <= #str then
      cap = str:sub(last_end)
      table.insert(t, cap)
   end
   return t
end